package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.common.R;
import com.itheima.dao.ISysMenuMapper;
import com.itheima.pojo.SysMenu;
import com.itheima.pojo.TreeSelect;
import com.itheima.service.SysMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class ISysMenuServiceImpl implements SysMenuService {

    @Autowired
    private ISysMenuMapper menuMapper;

    @Override
    public R menuTreeSelect() {
        LambdaQueryWrapper<SysMenu> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysMenu::getParentId, 0);
        List<SysMenu> sysMenus = menuMapper.selectList(wrapper);

        List<TreeSelect> list = new ArrayList<>();

        for (SysMenu sysMenu : sysMenus) {
            TreeSelect treeSelect = new TreeSelect();
            treeSelect.setId(sysMenu.getMenuId());
            treeSelect.setLabel(sysMenu.getMenuName());
            list.add(treeSelect);
        }

        for (TreeSelect treeSelect : list) {//系统管理
            LambdaQueryWrapper<SysMenu> wrapper1 = new LambdaQueryWrapper<>();
            wrapper1.eq(SysMenu::getParentId, treeSelect.getId());
            List<SysMenu> sysMenus1 = menuMapper.selectList(wrapper1);

            List<TreeSelect> child1 = new ArrayList<>();
            for (SysMenu sysMenu : sysMenus1) {
                TreeSelect treeSelect1 = new TreeSelect();
                treeSelect1.setId(sysMenu.getMenuId());
                treeSelect1.setLabel(sysMenu.getMenuName());
                child1.add(treeSelect1);
            }

            for (TreeSelect select : child1) {
                LambdaQueryWrapper<SysMenu> wrapper2 = new LambdaQueryWrapper<>();
                wrapper2.eq(SysMenu::getParentId, select.getId());
                List<SysMenu> sysMenus2 = menuMapper.selectList(wrapper2);

                if (sysMenus2 != null) {
                    List<TreeSelect> child2 = new ArrayList<>();
                    for (SysMenu sysMenu : sysMenus2) {
                        TreeSelect treeSelect1 = new TreeSelect();
                        treeSelect1.setId(sysMenu.getMenuId());
                        treeSelect1.setLabel(sysMenu.getMenuName());
                        child2.add(treeSelect1);
                    }

                    for (TreeSelect treeSelect1 : child2) {
                        LambdaQueryWrapper<SysMenu> wrapper3 = new LambdaQueryWrapper<>();
                        wrapper3.eq(SysMenu::getParentId, treeSelect1.getId());
                        List<SysMenu> sysMenus3 = menuMapper.selectList(wrapper3);
                        if (sysMenus3 != null) {
                            List<TreeSelect> child3 = new ArrayList<>();
                            for (SysMenu sysMenu : sysMenus3) {
                                TreeSelect treeSelect2 = new TreeSelect();
                                treeSelect2.setId(sysMenu.getMenuId());
                                treeSelect2.setLabel(sysMenu.getMenuName());
                                child3.add(treeSelect2);
                            }
                            treeSelect1.setChildren(child3);
                        }
                    }
                    select.setChildren(child2);
                }
            }
            treeSelect.setChildren(child1);
        }
        //System.out.println(list);
        return new R(1, "查询成功", list);
    }
}
